package com.alibaba.dingtalk.openapi.helper;

import com.alibaba.dingtalk.openapi.exception.OApiException;
import com.alibaba.dingtalk.openapi.utils.HttpHelper;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author kaka
 */
public class ServiceHelper {

	private static final Logger LOG = LoggerFactory.getLogger(ServiceHelper.class);
	
	public static String getSuiteToken(String suiteKey, String suiteSecret, String suiteTicket){
		String url = Env.OAPI_HOST + "/service/get_suite_token";
		JSONObject json = new JSONObject();
		json.put("suite_key", suiteKey);
		json.put("suite_secret", suiteSecret);
		json.put("suite_ticket", suiteTicket);
		JSONObject reponseJson = null;
		String suiteAccessToken = null;
		try {
			reponseJson = HttpHelper.httpPost(url,json);
			suiteAccessToken = reponseJson.getString("suite_access_token");
		} catch (OApiException e) {
			LOG.error("请求错误", e);
		}
		return suiteAccessToken;
	}
	
	public static JSONObject getPermanentCode(String tmpAuthCod, String suiteAccessToken ){
		String url = Env.OAPI_HOST + "/service/get_permanent_code?suite_access_token=" + suiteAccessToken ;
		JSONObject json = new JSONObject();
		json.put("tmp_auth_code", tmpAuthCod);
		JSONObject reponseJson = null;
		try {
			reponseJson = HttpHelper.httpPost(url,json);
		} catch (OApiException e) {
			LOG.error("请求错误", e);
		}
		return reponseJson;		
	}
	
	public static String getCorpToken(String authCorpid, String permanentCode, String suiteAccessToken ){
		String url = Env.OAPI_HOST + "/service/get_corp_token?suite_access_token=" + suiteAccessToken ;
		JSONObject json = new JSONObject();
		json.put("auth_corpid", authCorpid);
		json.put("permanent_code", permanentCode);
		JSONObject reponseJson = null;
		String corpToken = null;
		try {
			reponseJson = HttpHelper.httpPost(url,json);
			corpToken = reponseJson.getString("access_token");
			
		} catch (OApiException e) {
			LOG.error("请求错误", e);
		}
		return corpToken;		
	}
	
	public static JSONObject getAuthInfo(String suiteAccessToken, String suiteKey, String authCorpid, String permanentCode){
		String url = Env.OAPI_HOST + "/service/get_auth_info?suite_access_token=" + suiteAccessToken ;
		JSONObject json = new JSONObject();
		json.put("suite_key", suiteKey);
		json.put("auth_corpid", authCorpid);
		json.put("permanent_code", permanentCode);

		JSONObject reponseJson = null;
		try {
			reponseJson = HttpHelper.httpPost(url,json);
		} catch (OApiException e) {
			reponseJson = new JSONObject();
			reponseJson.put("errcode", e.getErrCode());
			reponseJson.put("errmsg", e.getErrMsg());
			if(e.getErrCode() == 41030) {
				LOG.warn("很可能是解除授权后同时收到change_auth和suite_relieve回调时导致的问题！", e);
			} else {
				LOG.error("获取公司授权信息时出错", e);
			}
			
		}
		return reponseJson;		
	}
	
	public static JSONObject getAgent(String suiteAccessToken, String suiteKey, String authCorpid, String permanentCode, String agentId){
		String url = Env.OAPI_HOST + "/service/get_agent?suite_access_token=" + suiteAccessToken ;
		JSONObject json = new JSONObject();
		json.put("suite_key", suiteKey);
		json.put("auth_corpid", authCorpid);
		json.put("permanent_code", permanentCode);
		//agentid可以通过getAuthInfo返回的json中得到
		json.put("agentid", agentId);

		JSONObject reponseJson = null;
		try {
			reponseJson = HttpHelper.httpPost(url,json);
		} catch (OApiException e) {
			LOG.error("请求错误", e);
		}
		return reponseJson;		
	}
	

	public static JSONObject getActivateSuite(String suiteAccessToken, String suiteKey, String authCorpid, String permanentCode ){
		String url = Env.OAPI_HOST + "/service/activate_suite?suite_access_token=" + suiteAccessToken ;
		JSONObject json = new JSONObject();
		json.put("suite_key", suiteKey);
		json.put("auth_corpid", authCorpid);
		json.put("permanent_code", permanentCode);

		JSONObject reponseJson = null;
		try {
			reponseJson = HttpHelper.httpPost(url,json);
		} catch (OApiException e) {
			LOG.error("请求错误", e);
		}
		return reponseJson;		
	}
}
